/**
 * A very simple company editor
 */
'use strict';
var Company = require('../../../models/companyModel');
String.prototype.zero4n = function () {
    return this ? this : 0;
};
module.exports = function (router) {

    /**
     * Retrieve a list of all companies for editing.
     */
    router.get('/', function (req, res) {

        Company.find(function (err, comps) {
            if (err) {
                console.log(err);
            }
            // comps.forEach(function(prod) {
            //         prod.prettyPrice = prod.prettyPrice();
            // });
            var model =
                    {
                        companies: comps
                    };
            res.render('admin/companies', model);                  // 'companies' 是dust的模块文件
        });

    });


    /**
     * Add a new company to the database.
     * **** PLEASE READ THE COMMENT BELOW! ****
     */
    router.post('/', function (req, res) {
        var user_name = req.body.user_name,

            company_name = req.body.company_name,
            en_name = req.body.en_name,
            description = req.body.description,      // 公司介绍
            validated = req.body.validated,       // 是否认证
            valid_time = req.body.valid_time,         // 认证时间

            prod_type = req.body.prod_type,        // 主营产品，行业
            main_brand = req.body.main_brand,       // 主营品牌
            mode = req.body.mode,             // 经营模式
            capital = req.body.capital,          // 注册资金
            reg_year = req.body.reg_year,            // 公司注册时间
            reg_addr = req.body.reg_addr,       // 公司注册地
            legal_person = req.body.legal_person,      // 法定代表人

            employee_amount = req.body.employee_amount,  // 员工人数
            sell_year = req.body.sell_year,         // 年销量额 （万元）
            export_year = req.body.export_year,       // 年出口额 （万元）
            prod_month = req.body.prod_month,        // 月产量
            bank_from = req.body.bank_from,         // 开户行
            bank_account = req.body.bank_account,      // 帐号
            main_prod = req.body.main_prod,         // 主营产品
            main_market = req.body.market,       // 主要市场 （大陆，欧美，港澳）
            oem = req.body.oem,              // 是否提供OEM

            link_man = req.body.link_man,          // 联系人
            link_man_gender = req.body.link_man_gender,   // 联系人职位
            tel = req.body.tel,
            mobile = req.body.mobile,
            fax = req.body.fax,
            addr = req.body.address,
            zip = req.body.zipcode,
            email = req.body.email,
            thumb = req.body.thumb,
            thumb1 = req.body.thumb1,
            thumb2 = req.body.thumb2,
            site_url = req.body.site_url,          // 站内地址
            homepage = req.body.homepage,          // 公司主页
            shop_url = req.body.shop_url;          // 公司网店

        //Some very lightweight input checking
        if (company_name === '') {
            res.redirect('/admin/companies#BadInput');
            return;
        }
        console.log('capital: ' + capital);
        var newCompany = new Company({
            user_name: user_name,
            /* display_order: Number, */

            company_name: company_name,
            en_name: en_name,
            description: description,      // 公司介绍
            validated: validated,       // 是否认证
            valid_time: valid_time,         // 认证时间

            prod_type: prod_type,        // 主营产品，行业
            main_brand: main_brand,       // 主营品牌
            mode: mode,             // 经营模式
            capital: capital,          // 注册资金
            reg_year: reg_year,            // 公司注册时间
            reg_addr: reg_addr,       // 公司注册地
            legal_person: legal_person,      // 法定代表人

            employee_amount: employee_amount,  // 员工人数
            sell_year: sell_year,         // 年销量额 （万元）
            export_year: export_year,       // 年出口额 （万元）
            prod_month: prod_month,        // 月产量
            bank_from: bank_from,         // 开户行
            bank_account: bank_account,      // 帐号
            main_prod: main_prod,         // 主营产品
            main_market: main_market,       // 主要市场 （大陆，欧美，港澳）
            oem: oem,              // 是否提供OEM

            link_man: link_man,          // 联系人
            link_man_gender: link_man_gender,   // 联系人职位
            tel: tel,
            mobile: mobile,
            fax: fax,
            addr: address,
            zip: zipcode,
            email: email,
            thumb: thumb,             // 公司
            thumb1: thumb1,
            thumb2: thumb2,
            site_url: site_url,          // 站内地址
            homepage: homepage,          // 公司主页
            shop_url: shop_url        // 公司网店

        });

        /* The call back recieves to more arguments ->company/s that is/are added to the database
         and number of rows that are affected because of save, which right now are ignored
         only errors object is consumed*/
        newCompany.save(function(err) {
            if(err) {
                console.log('save error', err);
            }

            res.redirect('/admin/companies');
        });
    });

    /**
     * Delete a company.
     * @paaram: req.body.item_id Is the unique id of the company to remove.
     */
    router.delete('/', function (req, res) {
        Company.remove({_id: req.body.item_id}, function (err) {
            if (err) {
                console.log('Remove error: ', err);
            }
            res.redirect('/admin/companies');
        });
    });

    router.get('/delete/:item_id', function (req, res) {
        Company.remove({_id: req.params.item_id}, function (err) {
            if (err) {
                console.log('Remove error: ', err);
            }
            res.redirect('/admin/companies');
        });
    });

    /**
     * Edit a company.
     * Not implemented here
     */
    router.put('/', function (req, res) {
        console.log('PUT received. Ignoring.');
        res.redirect('/admin/companies');
    });

};
